import { defineSeed } from "@/seed-application/types";
import feedback from "@/utils/feedback";
import { open } from "@/components/Popup";
import SetActions from "../components/SetActions.vue";
import SetDatas from "../components/SetDatas.vue";
import SetFields from '../components/SetFields.vue'
import GetActions from "../components/GetActions.vue";

export default defineSeed({
  name: 'organization',
  label: '组织',
  comment: '',
  icon: 'ep:grid',
  // mode: 'popup',
  checkLogin: true,
  checkAction: true,

  actions: {
    page: {
      label: '组织列表',
      type: 'page',
      icon: 'ep:plus',
      componentProps: {
        index: true,
        multiple: true,
        toolbar: '!page,lookup',
        row: 'remove'
      },
      declares: ['code','name','manager','expire_date','usable','loginable','enable','clerk_size','roles'],
      searches: ['id_eq','manager.username_like','manager.id_eq','expire_date_gt','expire_date_lt','code_eq,name_like','manager.name_like'],
      orders: ['code','name','manager.name'],
      stats: {stat1: {label: '统计个数1', declare: 'name'}}
    },
    lookup: {
      label: '组织树'
    },
    save: {
      label: '新增',
      type: 'save',
      declares: ['parent_id', 'code', 'name'],
      elements: {'parent_id': {element: 'LookupTree'}},
      layout: {
        cls: ['full'],
        style: {
          display: 'grid',
          gridTemplateColumns: '50px minmax(0, 1fr) 50px',
          gridTemplateRows: '50px minmax(0, 1fr) 50px',
          gridTemplateAreas: "'one two three' 'four five six' 'seven eight nine'"
        },
        children: [{
          type: 'save',
          cls: ['border'],
          style: {gridArea: 'five', padding: '10px'}
        }]
      },
      popupProps: {maxHeight: '300px', width: '40vw'}
    },
    // code: {
    //   label: '设置编码',
    //   type: 'set',
    //   layout: {
    //     cls: ['full'],
    //     style: {
    //       display: 'grid',
    //       gridTemplateColumns: '50px minmax(0, 1fr) 50px',
    //       gridTemplateRows: '50px minmax(0, 1fr) 50px',
    //       gridTemplateAreas: "'one two three' 'four five six' 'seven eight nine'"
    //     },
    //     children: [{
    //       type: 'set',
    //       cls: ['border'],
    //       style: {gridArea: 'five', padding: '10px'},
    //       componentProps: {actionName: '$actionName', seedName: '$seedName', microName: '$microName', id: '$id'}
    //     }]
    //   },
    //   popupProps: {maxHeight: '220px', width: '40vw'}
    // },
    name: {
      label: '设置名称',
      type: 'set',
      popupProps: {maxHeight: '120px', width: '40vw'}
    },
    expireDate: {
      label: '设置截止日期',
      type: 'set',
      popupProps: {width: '40vw'}
    },
    clerkSize: {
      label: '设置编制',
      type: 'set',
      popupProps: {maxHeight: '100px', width: '40vw'}
    },
    usable: {
      label: '设置启用',
      type: 'update',
      declares: ['usable', 'loginable', 'enable', 'reason'],
      popupProps: {maxHeight: '300px', width: '40vw'}
    },
    manager: {
      label: '设置负责人',
      type: 'set'
    },
    getRoles: {
      label: '查看角色',
      type: 'get'
    },
    roles: {
      label: '分配角色',
      type: 'set'
    },
    getActions: {
      label: '查看功能权限',
      onClick: async (action, unit, row) => {
        if(row) {          
          open({title: action.label, unit}, GetActions, {action, id: row.id})
        } else {
          feedback.alertSelect()
        }
      }
    },
    actions: {
      label: '分配功能权限',
      onClick: (action, unit, row) => {
        if(row) {
          open({title: action.label, unit, width: '25vw'}, SetActions, {action, id: row.id})
        } else {
          feedback.alertSelect()
        }
      }
    },
    datas: {
      label: '分配数据权限',
      onClick: (action, unit, row) => {
        if(row) {
          open({title: action.label, unit}, SetDatas, {action, id: row.id})
        } else {
          feedback.alertSelect()
        }
      }
    },
    fields: {
      label: '分配字段权限',
      onClick: (action, unit, row) => {
        if(row) {
          open({title: action.label, unit}, SetFields, {action, id: row.id})
        } else {
          feedback.alertSelect()
        }
      }
    },
    detail: {
      label: '详情',
      type: 'detail',
      layout: {
          cls: ['full'],
          style: {
            display: 'grid',
            gridTemplateColumns: '150px minmax(0, 1fr) 150px',
            gridTemplateRows: '150px minmax(0, 1fr) 150px',
            gridTemplateAreas: "'one two three' 'four five six' 'seven eight nine'"
          },
          children: [{
            type: 'detail',
            cls: ['border', 'p2'],
            style: {gridArea: 'five'},
            componentProps: {actionName: '$actionName', seedName: '$seedName', microName: '$microName', id: '$id'}
          }]
      },
      declares: ['id','manager','code','name','expire_date','usable','loginable','enable','clerk_size']
    },
    remove: {
      label: '删除',
      type: 'remove'
    }
  },

  columns: {
    id: {type: 'char', length: 17, required: true, label: 'ID'},
    code: {type: 'varchar', required: true, unique: true, length: 255, label: '编码'},
    name: {type: 'varchar', length: 64, required: true, max: 50, label: '名称'},
    parent_id: {type: 'ref', label: '上级组织'},
    manager_id: {type: 'ref', length: 17, label: '负责人'},
    expire_date: {type: 'date', label: '截止日期'},
    usable: {type: 'yesno', length: 8, label: '是否可用'},
    loginable: {type: 'yesno', length: 8, label: '是否能登录'},
    enable: {type: 'yesno', length: 8, label: '是否启用'},
    reason: {type: 'text', label: '原因'},
    clerk_size: {type: 'int', label: '编制'},
    tenant_id: {type: 'char', length: 17, label: '所属租户'},
    created_at: {type: 'datetime', label: '创建时间'},
    updated_at: {type: 'datetime', label: '更新时间'},
    deleted_at: {type: 'datetime', label: '删除时间'},
    version: {type: 'int', label: '版本号'}
  },

  belongsTo: {
    manager: {seedName: 'user', selfColumn: 'manager_id', label: "负责人"},
    parent: {seedName: 'organization', selfColumn: 'parent_id', label: '上级组织'}
  },
  habtm: {
    roles: {seedName: 'role', throughSeedName: 'organizationRole', label: '拥有角色'}
  }
})
